//
//  MeetingrongRTCEngineDelegateImpl.m
//  RongRTCMeeting
//
//  Created by LiuLinhong on 2017/08/14.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "MeetingRongRTCEngineDelegateImpl.h"
#import "MeetingViewController.h"
#import "AppDelegate.h"
#import "SwitchPageViewControl.h"
#import "CommonUtility.h"
#import "LogonViewController.h"
#import "MeetingLocalDataInfoModel.h"
#import "MeetingMemberDataInfoModel.h"
#import "SwitchPageViewCell.h"

@interface MeetingRongRTCEngineDelegateImpl ()
{
    NSTimer *endMessageTipTimer;
    dispatch_queue_t queue;
}
@property (nonatomic, strong) MeetingViewController *meetingViewController;

@end


@implementation MeetingRongRTCEngineDelegateImpl


- (instancetype)initWithViewController:(NSViewController *)vc
{
    self = [super init];
    if (self)
    {
        self.meetingViewController = (MeetingViewController *) vc;
        self.meetingViewController.semaphore = dispatch_semaphore_create(0);
        queue = dispatch_queue_create("MeetingrongRTCEngineDelegateImpl", DISPATCH_QUEUE_SERIAL);
    }
    
    return self;
}

#pragma mark - rongRTCEngineDelegate
- (void)rongRTCEngine:(RongRTCEngine *)engine onConnectionStateChanged:(RongRTCConnectionState)state
{
    switch (state)
    {
        case RongRTC_ConnectionState_Connected:
            [self.meetingViewController modifySignalLevel:0];
            break;
        case RongRTC_ConnectionState_Disconnected:
            [self.meetingViewController modifySignalLevel:-1];
            break;
        default:
            break;
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onJoinComplete:(BOOL)success
{
    DLog(@"LLH...... onJoinComplete: %zd", success);
//    self.meetingViewController.meetingViewBuilder.localVideoView = [engine createLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
//    AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
//    [appDelegate.childWindow.contentView addSubview:self.meetingViewController.meetingViewBuilder.localVideoView];
//    [engine closeLocalVideo:!self.meetingViewController.closeCameraIndex];
    
    if (success)
    {
        RongRTCUserType type = [engine getUserType];
        [self.meetingViewController loadLocalVideoView];
        [engine requestWhiteBoardExist];
        LarkMeetingManager.observerIndex = type;
        [self.meetingViewController addTipMsg:@"" withMessageType:RongRTC_Message_Alone];
        if (type == RongRTC_User_Host)
            [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToHost];
        
        NSInteger avType = [self.meetingViewController.paraDic[kCloseCamera] integerValue];
//        if (avType == RongRTC_User_Only_Audio && self.meetingViewController.isFinishLeave)
//        {
//            [self.meetingViewController modifyAudioVideoType:self.meetingViewController.chatViewBuilder.openCameraButton];
//            self.meetingViewController.isFinishLeave = NO;
//        }
        
        if (avType == RongRTC_User_Only_Audio)
        {
//            [self.meetingViewController modifyAudioVideoType];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.meetingViewController closeCameraButtonPressed];
            });
        }
        
        ///////////////////////////////////////////
        
        MeetingVideoViewModel *model = [[MeetingVideoViewModel alloc] initWithView:nil];
        model.userID = self.meetingViewController.userID;
        model.userName = self.meetingViewController.userName;
        model.userType = type;
        model.avType = avType;
        NSColor *headColor = (type == RongRTC_User_Observer) ? [NSColor observerColor] : [NSColor randomColorForAvatarRBG];
        model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
        model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
        
//        if (avType == RongRTC_User_Only_Audio)
//        {
//            NSImageView *micphoneImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, kCellSize, kCellSize)];
//            micphoneImageView.image = [NSImage imageNamed:@"MeetingCloseCameraHeaderImage"];
//            [remoteVideoView addSubview:micphoneImageView];
//            model.micImageView = micphoneImageView;
//        }
        
        [LarkMeetingManager addRemoteAllMemberArrayObject:model];
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        //////////////////////////////////
    }
//    else
//        [self.meetingViewController hungupButtonPressed];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onLeaveComplete:(BOOL)success
{
    DLog(@"LLH...... onLeaveComplete: %zd", success);
    [self.meetingViewController.durationTimer invalidate];
    self.meetingViewController.durationTimer = nil;
    self.meetingViewController.meetingViewBuilder.durationTimeTextField.stringValue = @"";
    [self.meetingViewController leaveChannel];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onUserJoined:(NSString *)userId userName:(NSString *)userName userType:(RongRTCUserType)type audioVideoType:(RongRTCAudioVideoType)avType
{
    DLog(@"LLH...... onUserJoined userId: %@ userName: %@ RongRTCUserType: %zd  audioVideoType: %zd", userId, userName, type, avType);
    if (self.meetingViewController.duration == 0 && !self.meetingViewController.durationTimer)
    {
        self.meetingViewController.meetingViewBuilder.durationTimeTextField.stringValue = @"00:00";
        self.meetingViewController.durationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self.meetingViewController selector:@selector(updateTalkTimeLabel) userInfo:nil repeats:YES];
    }
    
    if (type == RongRTC_User_Observer)
    {
        [LarkMeetingManager addUserIDArrayObject:userId];
        
        MeetingVideoViewModel *meetingVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:nil];
        meetingVideoViewModel.userID = userId;
        meetingVideoViewModel.userName = userName;
        meetingVideoViewModel.userType = type;
        meetingVideoViewModel.avType = avType;
        NSColor *headColor = [NSColor observerColor];
        meetingVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:meetingVideoViewModel.userName withColor:headColor];
        meetingVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:meetingVideoViewModel.userName withColor:headColor];
        meetingVideoViewModel.micImageView = meetingVideoViewModel.switchHeadIconView;
        [LarkMeetingManager addRemoteAllMemberArrayObject:meetingVideoViewModel];
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        
        return;
    }
    
    if (LarkMeetingManager.observerIndex == RongRTC_User_Observer && (!self.meetingViewController.localVideoViewModel || [self.meetingViewController.localVideoViewModel.userID isEqualToString:@""]))
    {
        [self.meetingViewController addTipMsg:self.meetingViewController.userName withMessageType:RongRTC_Message_Join];
//        self.meetingViewController.meetingViewBuilder.localVideoView = [engine createRemoteVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) forUser:userId withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
//        
//        AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
//        [appDelegate.childWindow.contentView addSubview:self.meetingViewController.meetingViewBuilder.localVideoView];
        
        if ([LarkMeetingManager isUserIDArrayContainObject:userId])
            [LarkMeetingManager removeUserIDArrayObject:userId];
        
        [LarkMeetingManager addUserIDArrayObject:userId];
        
        if (!self.meetingViewController.localVideoViewModel)
            self.meetingViewController.localVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:self.meetingViewController.meetingViewBuilder.localVideoView];
        
        self.meetingViewController.localVideoViewModel.userID = userId;
        self.meetingViewController.localVideoViewModel.userName = userName;
        self.meetingViewController.localVideoViewModel.userType = type;
        self.meetingViewController.localVideoViewModel.avType = avType;
        NSColor *headColor = (type == RongRTC_User_Observer) ? [NSColor observerColor] : [NSColor randomColorForAvatarRBG];
        self.meetingViewController.localVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
        self.meetingViewController.localVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
        self.meetingViewController.localVideoViewModel.micImageView = self.meetingViewController.localVideoViewModel.switchHeadIconView;
        
        [LarkMeetingManager addRemoteAllMemberArrayObject:self.meetingViewController.localVideoViewModel];
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        
        if (avType == RongRTC_User_Only_Audio || avType == RongRTC_User_Audio_Video_None)
            [self.meetingViewController.meetingViewBuilder.localVideoView addSubview:self.meetingViewController.localVideoViewModel.micImageView];
    }
    else
    {
        [self.meetingViewController addTipMsg:userName withMessageType:RongRTC_Message_Join];
        
        if ([LarkMeetingManager isUserIDArrayContainObject:userId])
            [LarkMeetingManager removeUserIDArrayObject:userId];
        
        [LarkMeetingManager addUserIDArrayObject:userId];
        
        BOOL isExistUser = NO;
        NSInteger existIndex = 0;
        for (NSInteger i = 0; i < [LarkMeetingManager.remoteAllMemberArray count]; i++)
        {
            MeetingVideoViewModel *tmpModel = (MeetingVideoViewModel *)LarkMeetingManager.remoteAllMemberArray[i];
            if ([tmpModel.userID isEqualToString:userId])
            {
                isExistUser = YES;
                existIndex = i;
            }
        }
        
        MeetingVideoViewModel *meetingVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:nil];
        if (!isExistUser)
            [LarkMeetingManager addRemoteAllMemberArrayObject:meetingVideoViewModel];
        else
            [LarkMeetingManager replaceObjectInRemoteAllMemberArrayAtIndex:existIndex withObject:meetingVideoViewModel];
        
        meetingVideoViewModel.userID = userId;
        meetingVideoViewModel.userName = userName;
        meetingVideoViewModel.userType = type;
        meetingVideoViewModel.avType = avType;
        NSColor *headColor = (type == RongRTC_User_Observer) ? [NSColor observerColor] : [NSColor randomColorForAvatarRBG];
        meetingVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:meetingVideoViewModel.userName withColor:headColor];
        meetingVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:meetingVideoViewModel.userName withColor:headColor];
        meetingVideoViewModel.micImageView = meetingVideoViewModel.switchHeadIconView;
        
        [LarkMeetingManager addRemoteVideoViewArrayObject:meetingVideoViewModel];
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        
        NSInteger userIndex = [LarkMeetingManager.remoteVideoViewArray count] - 1;
        NSInteger page = 0;
        NSInteger cell = 0;
        if (self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage > 0)
        {
            page = userIndex / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
            cell = fmod(userIndex, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
        }
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
        [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
        
//        NSInteger count = [self.meetingViewController.remoteVideoViewArray count];
//        NSInteger page = 0;
//        NSInteger cell = 0;
//        if (self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage > 0)
//        {
//            page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
//            cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
//        }
//        
//        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
//        [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
//        
//        if (!self.meetingViewController.isSwitchVideoView && [self.meetingViewController.userIDArray count] == 1)
//            [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
    }
    
    ////////////////////////////////
    /*
    {
        [self.meetingViewController addTipMsg:userId withMessageType:RongRTC_Message_Join];
        [self.meetingViewController.userIDArray addObject:userId];
        
        NSView *remoteVideoView = [engine createRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) forUser:userId withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
        MeetingVideoViewModel *meetingVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:remoteVideoView];
        meetingVideoViewModel.userID = userId;
        meetingVideoViewModel.userType = type;
        meetingVideoViewModel.avType = avType;
        meetingVideoViewModel.headerImage = [NSImage imageNamed:@"MeetingMemberNormal"];
        
        if (avType == RongRTC_User_Only_Audio)
        {
            NSImageView *micphoneImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, kCellSize, kCellSize)];
            micphoneImageView.image = [NSImage imageNamed:@"MeetingCloseCameraHeaderImage"];
            [remoteVideoView addSubview:micphoneImageView];
            meetingVideoViewModel.micImageView = micphoneImageView;
        }
        
        BOOL isExistUser = NO;
        for (NSInteger i = 0; i < [self.meetingViewController.remoteAllMemberArray count]; i++)
        {
            MeetingVideoViewModel *tmpModel = (MeetingVideoViewModel *)self.meetingViewController.remoteAllMemberArray[i];
            if ([tmpModel.userID isEqualToString:userId])
            {
                isExistUser = YES;
                [self.meetingViewController.remoteAllMemberArray replaceObjectAtIndex:i withObject:meetingVideoViewModel];
            }
        }
        
        if (!isExistUser)
            [self.meetingViewController.remoteAllMemberArray addObject:meetingVideoViewModel];
        
        [self.meetingViewController.remoteVideoViewArray addObject:meetingVideoViewModel];
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        
        NSInteger count = [self.meetingViewController.remoteVideoViewArray count];
        NSInteger page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
        NSInteger cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
        [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
    }
     */
    //////////////////////////
}

- (void)rongRTCEngine:(RongRTCEngine *)engine OnNotifyUserVideoCreated:(NSString *)userId
{
    if (LarkMeetingManager.observerIndex == RongRTC_User_Observer && [self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
    {
        CGRect rect = LarkMeetingManager.isFullScreen ? CGRectMake(0, 0, kScreenWidth, kScreenHeight) : CGRectMake(0, 0, kVideoWidth, kVideoHeight);
        self.meetingViewController.meetingViewBuilder.localVideoView = [engine createRemoteVideoViewFrame:rect forUser:userId withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
        
        AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
        [appDelegate.childWindow.contentView addSubview:self.meetingViewController.meetingViewBuilder.localVideoView];
        
        [self.meetingViewController.rongRTCEngine subscribeStreamForTiny:LarkMeetingManager.userIDArray forOrigin:self.meetingViewController.localVideoViewModel.userID];
    }
    else
    {
        if ([LarkMeetingManager isUpgradeUserIDArrayContainObject:userId] || [self.meetingViewController.requestUpgradeUserID isEqualToString:userId])
        {
            BOOL isExistUser = NO;
            NSInteger existIndex = 0;
            MeetingVideoViewModel *tmpModel;
            for (NSInteger i = 0; i < [LarkMeetingManager.remoteAllMemberArray count]; i++)
            {
                tmpModel = (MeetingVideoViewModel *)LarkMeetingManager.remoteAllMemberArray[i];
                if ([tmpModel.userID isEqualToString:userId])
                {
                    isExistUser = YES;
                    existIndex = i;
                    break;
                }
            }
            
            MeetingVideoViewModel *meetingVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:nil];
            if (!isExistUser)
                [LarkMeetingManager addRemoteAllMemberArrayObject:meetingVideoViewModel];
            else
                [LarkMeetingManager replaceObjectInRemoteAllMemberArrayAtIndex:existIndex withObject:meetingVideoViewModel];
            
            meetingVideoViewModel.userID = userId;
            meetingVideoViewModel.userName = tmpModel.userName;
            meetingVideoViewModel.userType = RongRTC_User_Normal;
            meetingVideoViewModel.avType = RongRTC_User_Audio_Video;
            NSColor *headColor = [NSColor randomColorForAvatarRBG];
            meetingVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:meetingVideoViewModel.userName withColor:headColor];
            meetingVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:meetingVideoViewModel.userName withColor:headColor];
            meetingVideoViewModel.micImageView = meetingVideoViewModel.switchHeadIconView;
            
            [LarkMeetingManager addRemoteVideoViewArrayObject:meetingVideoViewModel];
            [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        }
        
        NSView *remoteVideoView = [engine createRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) forUser:userId withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
        MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
        if (model)
            model.cellVideoView = remoteVideoView;
        
        NSInteger userIndex = -1;
        for (MeetingVideoViewModel *model in LarkMeetingManager.remoteVideoViewArray)
        {
            userIndex++;
            if ([model.userID isEqualToString:userId])
            {
                model.cellVideoView = remoteVideoView;
                break;
            }
        }
        
        NSInteger page = 0;
        NSInteger cell = 0;
        if (self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage > 0)
        {
            page = userIndex / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
            cell = fmod(userIndex, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
        }
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
        if ([LarkMeetingManager isUpgradeUserIDArrayContainObject:userId] || [self.meetingViewController.requestUpgradeUserID isEqualToString:userId])
        {
            [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
            self.meetingViewController.requestUpgradeUserID = nil;
        }

        [self.meetingViewController.meetingViewBuilder.switchPageViewControl loadCellVideoView:indexPath withUserID:userId];
        
        if (!LarkMeetingManager.isSwitchVideoView && [LarkMeetingManager.userIDArray count] == 1)
            [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
        else
        {
            NSString *bigStreamUseID = self.meetingViewController.selectedViewModel.userID;
            if (!bigStreamUseID)
                bigStreamUseID = self.meetingViewController.userID;

            NSMutableArray *tinyStreamUseIDs = [NSMutableArray arrayWithArray:LarkMeetingManager.userIDArray];
            if(![bigStreamUseID isEqualToString:self.meetingViewController.userID])
                [tinyStreamUseIDs removeObject:bigStreamUseID];
            else
                bigStreamUseID = nil;

            [self.meetingViewController.rongRTCEngine subscribeStreamForTiny:tinyStreamUseIDs forOrigin:bigStreamUseID];
        }
        
        [LarkMeetingManager removeUpgradeUserIDArrayObject:userId];
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onUserLeft:(NSString *)userId
{
    if ([LarkMeetingManager.userIDArray indexOfObject:userId] != NSNotFound)
    {
        NSInteger index = [LarkMeetingManager.userIDArray indexOfObject:userId];
        [LarkMeetingManager removeObjectFromUserIDArrayAtIndex:index];
        if ([LarkMeetingManager.userIDArray count] == 0)
        {
            if (self.meetingViewController.durationTimer)
            {
                [self.meetingViewController.durationTimer invalidate];
                self.meetingViewController.duration = 0;
                self.meetingViewController.durationTimer = nil;
            }
            
            self.meetingViewController.meetingViewBuilder.durationTimeTextField.stringValue = @"00:00";
        }
        
        NSInteger page = 0;
        if (self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage > 0)
            page = index / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
            
        NSInteger cell = fmod(index, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
        
        if ([LarkMeetingManager.userIDArray count] == 0 && LarkMeetingManager.isSwitchVideoView)
            self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake((kVideoWidth-kCellSize)/2, (kVideoHeight-kCellSize)/2, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
        
        MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
        if (model)
        {
            if (model.userType != RongRTC_User_Observer && LarkMeetingManager.isSwitchVideoView && LarkMeetingManager.selectedSwitchCellIndex == cell && LarkMeetingManager.selectedSwitchPageIndex == page)
            {
                [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
            }
            
            [self.meetingViewController.rightMVTableViewDelegateSource.itemDic removeObjectForKey:userId];
            [LarkMeetingManager removeRemoteAllMemberArrayObject:model];
            [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        }
        
        model = [LarkMeetingManager getRemoteVideoViewArrayObject:userId];
        if ((model.userType == RongRTC_User_Normal || model.userType == RongRTC_User_Host))
        {
            [self.meetingViewController addTipMsg:model.userName withMessageType:RongRTC_Message_Leave];
            [LarkMeetingManager removeObjectFromRemoteVideoViewArrayWithUserID:userId];
            [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:indexPath];
        }
        
        if (LarkMeetingManager.observerIndex == RongRTC_User_Host)
            [self.meetingViewController endBreathTimerAction];
    }
    
    if (LarkMeetingManager.observerIndex == RongRTC_User_Observer)
    {
        if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
        {
            [self.meetingViewController.localVideoViewModel.cellVideoView removeFromSuperview];
            
            if ([LarkMeetingManager.remoteVideoViewArray count] > 0)
            {
                self.meetingViewController.localVideoViewModel = LarkMeetingManager.remoteVideoViewArray[0];
                [LarkMeetingManager removeObjectFromRemoteVideoViewArrayAtIndex:0];
                [LarkMeetingManager removeObjectFromUserIDArrayAtIndex:0];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:[NSIndexPath indexPathForItem:0 inSection:0]];
                
                self.meetingViewController.localVideoViewModel.cellVideoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
                self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
                AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
                [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
            }
            else if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
            {
                self.meetingViewController.localVideoViewModel = nil;
                
                if (self.meetingViewController.durationTimer)
                {
                    [self.meetingViewController.durationTimer invalidate];
                    self.meetingViewController.duration = 0;
                    self.meetingViewController.durationTimer = nil;
                }
                
                self.meetingViewController.meetingViewBuilder.durationTimeTextField.stringValue = @"00:00";
            }
            
            MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
            if (model)
            {
                [LarkMeetingManager removeRemoteAllMemberArrayObject:model];
                [self.meetingViewController.rightMVTableViewDelegateSource.itemDic removeObjectForKey:userId];
                
                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
                
                if (model.userType == RongRTC_User_Normal || model.userType == RongRTC_User_Host)
                    [self.meetingViewController addTipMsg:userId withMessageType:RongRTC_Message_Leave];
            }
        }
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine OnNotifyUserVideoDestroyed:(NSString *)userId
{
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onUser:(NSString *)userId audioVideoType:(RongRTCAudioVideoType)avType
{
    if (LarkMeetingManager.observerIndex == RongRTC_User_Observer && [self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
    {
        self.meetingViewController.localVideoViewModel.avType = avType;
        RongRTCVideoViewDisplayType type;
        if (self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.width == kVideoWidth && self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.height == kVideoHeight)
            type = RongRTC_VideoViewDisplay_CompleteView;
        else
            type = RongRTC_VideoViewDisplay_FullScreen;
        
        NSView *videoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.width, self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.height) withUserID:userId withDisplayType:type];
        self.meetingViewController.localVideoViewModel.cellVideoView = videoView;
        
        
        if (avType == RongRTC_User_Only_Audio || avType == RongRTC_User_Audio_Video_None)
        {
            if (videoView.frame.size.width == kVideoWidth && videoView.frame.size.height == kVideoHeight)
                self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake((kVideoWidth-kCellSize)/2, (kVideoHeight-kCellSize)/2, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
            else if (videoView.frame.size.width == kScreenWidth && videoView.frame.size.height == kScreenHeight)
            {
                self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake((kScreenWidth-kCellSize)/2, (kScreenHeight-kCellSize)/2, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
            }
            else
                self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
            
            [videoView addSubview:self.meetingViewController.localVideoViewModel.micImageView];
        }
        else
        {
            if (self.meetingViewController.localVideoViewModel.micImageView)
                [self.meetingViewController.localVideoViewModel.micImageView removeFromSuperview];
        }
        
        return;
    }
    
    MeetingVideoViewModel *model = [LarkMeetingManager getRemoteVideoViewArrayObject:userId];
    if (model)
    {
        RongRTCVideoViewDisplayType type;
        if ((model.cellVideoView.frame.size.width == kVideoWidth && model.cellVideoView.frame.size.height == kVideoHeight) || (model.cellVideoView.frame.size.width == kScreenWidth && model.cellVideoView.frame.size.height == kScreenHeight))
            type = RongRTC_VideoViewDisplay_CompleteView;
        else
            type = RongRTC_VideoViewDisplay_FullScreen;
        
        NSView *videoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, model.cellVideoView.frame.size.width, model.cellVideoView.frame.size.height) withUserID:userId withDisplayType:type];
        model.cellVideoView = videoView;
        
        if (avType == RongRTC_User_Only_Audio || avType == RongRTC_User_Audio_Video_None)
        {
            if (videoView.frame.size.width == kVideoWidth && videoView.frame.size.height == kVideoHeight)
            {
                model.micImageView.frame = CGRectMake((kVideoWidth-kCellSize)/2, (kVideoHeight-kCellSize)/2, model.micImageView.frame.size.width, model.micImageView.frame.size.height);
            }
            else if (videoView.frame.size.width == kScreenWidth && videoView.frame.size.height == kScreenHeight)
            {
                model.micImageView.frame = CGRectMake((kScreenWidth-kCellSize)/2, (kScreenHeight-kCellSize)/2, model.micImageView.frame.size.width, model.micImageView.frame.size.height);
            }
            else
            {
                model.micImageView.frame = CGRectMake(0, 0, model.micImageView.frame.size.width, model.micImageView.frame.size.height);
            }
            
            [videoView addSubview:model.micImageView];
        }
        else
        {
            if (model.micImageView)
                [model.micImageView removeFromSuperview];
        }
        
        model.avType = avType;
    }
    
    MeetingVideoViewModel *memberModel = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
    if (memberModel)
        memberModel.avType = avType;
    
    [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onWhiteBoardURL:(NSString *)url
{
    DLog(@"LLH...... onWhiteBoardURL: %@", url);
    
    if (LarkMeetingManager.is3UseWhiteBoard)
    {
        [[self.meetingViewController.meetingViewBuilder.whiteBoardWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
        [self.meetingViewController.mainView addSubview:self.meetingViewController.meetingViewBuilder.whiteBoardWebView positioned:NSWindowBelow relativeTo:self.meetingViewController.meetingViewBuilder.rightContentView];
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onWhiteBoardExist:(BOOL)isExist
{
    LarkMeetingManager.isWhiteBoardExist = isExist;
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyWhiteBoardCreateBy:(NSString *)userId
{
    MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
    if (model)
        [self.meetingViewController addTipMsg:model.userName withMessageType:RongRTC_Message_WhiteBoard];
    
    LarkMeetingManager.isWhiteBoardExist = YES;
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onControlAudioVideoDevice:(NSInteger)code
{
    
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyControlAudioVideoDevice:(RongRTC_Device_Type)type withUserID:(NSString *)userId open:(BOOL)isOpen
{
    [self updateUser:userId deviceType:type open:isOpen];
}

#pragma mark - 会议控制
- (void)rongRTCEngine:(RongRTCEngine *)engine onObserverRequestBecomeNormalUser:(NSInteger)code
{
    
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onGetInviteURL:(NSString *)url responseCode:(NSInteger)code
{
    DLog(@"LLH...... onInviteURL: %@", url);
    
    NSString *pasteMessage = [NSString stringWithFormat:@"会议邀请:\n\n请输入以下会议ID加入会议\n会议ID: %@\n\n如果您尚未安装客户端, 请使用Chrome或Safari 11 打开以下地址加入会议:\n会议地址: %@", self.meetingViewController.channelID, url];
    NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
    [pasteBoard clearContents];
    [pasteBoard setString:pasteMessage forType:NSPasteboardTypeString];
    
    [self.meetingViewController showMessageAlertSingle:NSLocalizedString(@"meeting_msg_15", nil) title:NSLocalizedString(@"meeting_invite", nil)];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNormalUserRequestHostAuthority:(NSInteger)code
{
    DLog(@"LLH...... onNormalUserRequestHostAuthority: %zd", code);
    
    if (code != 0)
        return;
    
    LarkMeetingManager.observerIndex = RongRTC_User_Host;
    [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToHost];
    for (MeetingVideoViewModel *model in LarkMeetingManager.remoteAllMemberArray)
    {
//        NSColor *headColor = [NSColor randomColorForAvatarRBG];
        if ([model.userID isEqualToString:self.meetingViewController.userID])
        {
            model.userType = RongRTC_User_Host;
//            model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
//            model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
        }
        else if (model.userType == RongRTC_User_Host)
        {
            model.userType = RongRTC_User_Normal;
//            model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
//            model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
        }
    }
    
    [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onDegradeNormalUserToObserver:(NSInteger)code
{
    
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onUpgradeObserverToNormalUser:(NSInteger)code
{
    
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyDegradeNormalUserToObserver:(NSString *)userId
{
//    [self.meetingViewController addTipMsg:userId withMessageType:RongRTC_Message_Degrade];
    //右侧成员列表
    MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
    if (model)
    {
        model.userType = RongRTC_User_Observer;
        model.avType = RongRTC_User_Only_Audio;
        NSColor *headColor = [NSColor observerColor];
        model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
        model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
    }
    
    [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
    
    if ([userId isEqualToString:self.meetingViewController.userID]) //自己被降级为观察者
    {
        [self.meetingViewController restoreCloseCameraState];
        [self.meetingViewController restoreMuteState];
        [engine answerDegradeNormalUserToObserver:userId status:YES];
        
        if (LarkMeetingManager.is4ShareScreen)
            [self.meetingViewController shareScreenButtonPressed];
        
        if (LarkMeetingManager.isSwitchVideoView)
        {
            NSIndexPath *formerIndexPath = [NSIndexPath indexPathForItem:LarkMeetingManager.selectedSwitchCellIndex inSection:LarkMeetingManager.selectedSwitchPageIndex];
            [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:formerIndexPath];
        }
        
        //视频处理
        [self.meetingViewController.localVideoViewModel.cellVideoView removeFromSuperview];
        NSInteger remoteVideoViewIndex = [LarkMeetingManager.remoteVideoViewArray count] - 1;
        self.meetingViewController.localVideoViewModel = [LarkMeetingManager.remoteVideoViewArray lastObject];
        
        [LarkMeetingManager removeUserIDArrayObject:self.meetingViewController.localVideoViewModel.userID];
        
        CGRect rect = LarkMeetingManager.isFullScreen ? CGRectMake(0, 0, kScreenWidth, kScreenHeight) : CGRectMake(0, 0, kVideoWidth, kVideoHeight);
        self.meetingViewController.localVideoViewModel.cellVideoView = [engine changeRemoteVideoViewFrame:rect withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
        
        AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
        [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
        [LarkMeetingManager removeObjectFromRemoteVideoViewArrayAtIndex:remoteVideoViewIndex];
        
        NSInteger page = remoteVideoViewIndex / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
        NSInteger cell = fmod(remoteVideoViewIndex, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
        [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:indexPath];
        LarkMeetingManager.observerIndex = RongRTC_User_Observer;
        [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToObserver];
        
        if (LarkMeetingManager.is3UseWhiteBoard)
            [self.meetingViewController.rongRTCEngine requestWhiteBoardURL];
        
        NSString *bigStreamUseID = self.meetingViewController.localVideoViewModel.userID;
        NSMutableArray *tinyStreamUseIDs = [NSMutableArray arrayWithArray:LarkMeetingManager.userIDArray];
        [tinyStreamUseIDs removeObject:bigStreamUseID];
        [self.meetingViewController.rongRTCEngine subscribeStreamForTiny:tinyStreamUseIDs forOrigin:bigStreamUseID];
    }
    else //其他人被降级为观察者
    {
        //视频需要在此处被移除
        if ([LarkMeetingManager.userIDArray indexOfObject:userId] != NSNotFound)
        {
//            [self.meetingViewController addTipMsg:userId withMessageType:RongRTC_Message_Degrade];
            NSInteger index = [LarkMeetingManager.userIDArray indexOfObject:userId];
//            [self.meetingViewController.userIDArray removeObjectAtIndex:index];
            //        [weakChatVC resumeLocalView:indexPath];
            
            //        MeetingVideoViewModel *degradeModel;
            //        for (MeetingVideoViewModel *model in self.meetingViewController.remoteAllMemberArray)
            //        {
            //            if ([model.userID isEqualToString:userId])
            //            {
            //                degradeModel = model;
            ////                [self.meetingViewController.remoteAllMemberArray removeObject:model];
            //                break;
            //            }
            //        }
            ////        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
            //
            ////        if (degradeModel.userType == RongRTC_User_Normal)
            {
                [LarkMeetingManager removeObjectFromRemoteVideoViewArrayAtIndex:index];
                NSInteger page = index / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
                NSInteger cell = fmod(index, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:indexPath];
            }
        }
        
        if (LarkMeetingManager.observerIndex == RongRTC_User_Observer)
        {
            if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
            {
                [self.meetingViewController.localVideoViewModel.cellVideoView removeFromSuperview];
                
                if ([LarkMeetingManager.remoteVideoViewArray count] > 0)
                {
                    self.meetingViewController.localVideoViewModel = LarkMeetingManager.remoteVideoViewArray[0];
                    [LarkMeetingManager removeObjectFromRemoteVideoViewArrayAtIndex:0];
//                    [self.meetingViewController.userIDArray removeObjectAtIndex:0];
                    //                [weakChatVC.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
                    [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:[NSIndexPath indexPathForItem:0 inSection:0]];
                    
                    self.meetingViewController.localVideoViewModel.cellVideoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
                    self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
                    AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
                    [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
                }
                else if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
                {
                    self.meetingViewController.localVideoViewModel = nil;
                }
            }
        }
    }
}

//暂时未使用, 无用实现LLH
//- (void)rongRTCEngine:(rongRTCEngine *)engine onRoleChange:(NSString *)userID host:(NSString *)hostId roleType:(RongRTC_Capacity_Type)type
//{
//    switch (type)
//    {
//        case RongRTC_Capacity_Degrade: //降级为观察者
//        {
//            if ([userID isEqualToString:self.meetingViewController.userID]) //自己被降级
//            {
//                /*
//                [self.meetingViewController.remoteAllMemberArray removeAllObjects];
//                [self.meetingViewController.remoteVideoViewArray removeAllObjects];
//                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
//                [self.meetingViewController.meetingViewBuilder.switchPageViewControl reloadCellInPageView];
//                
//                //将自己退出后, 重新joinchannel降级为观察者
//                self.meetingViewController.localVideoViewModel = nil;
//                self.meetingViewController.observerIndex = RongRTC_User_Observer;
//                NSUserDefaults *settingUserDefaults = [LogonViewController shareSettingUserDefaults];
//                [settingUserDefaults setObject:@(RongRTC_User_Observer) forKey:Key_Observer];
//                [settingUserDefaults synchronize];
//                self.meetingViewController.isBecomeNormalUser = YES;
//                [engine leaveChannel];
//                 */
//                
//                //右侧成员列表
//                for (MeetingVideoViewModel *model in self.meetingViewController.remoteAllMemberArray)
//                {
//                    if ([model.userID isEqualToString:userID])
//                    {
//                        model.userType = RongRTC_User_Observer;
//                        model.avType = RongRTC_User_Audio_Video;
//                        model.headerImage = [NSImage imageNamed:@"MeetingMemberObserver"];
//                        break;
//                    }
//                }
//                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
//            }
//            else //其他人的处理
//            {
//            }
//        }
//            break;
//        case RongRTC_Capacity_Upgrade: //升级为正常用户
//        {
//            if (self.meetingViewController.observerIndex != RongRTC_User_Observer)
//                return;
//            
//            [self.meetingViewController startCallingBreath:^(BOOL isAccept) {
//                if (isAccept)
//                {
//                    [engine answerUserRequestResult:userID withAnswerType:RongRTC_Answer_Upgrade withDeviceType:RongRTC_Device_None status:isAccept];
//                    if ([userID isEqualToString:self.meetingViewController.userID])
//                    {
//                        //右侧成员列表
//                        for (MeetingVideoViewModel *model in self.meetingViewController.remoteAllMemberArray)
//                        {
//                            if ([model.userID isEqualToString:userID])
//                            {
//                                model.userType = RongRTC_User_Normal;
//                                model.avType = RongRTC_User_Audio_Video;
//                                model.headerImage = [NSImage imageNamed:@"MeetingMemberNormal"];
//                                break;
//                            }
//                        }
//                        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
//                        
//                        //视频VideoViewArray中添加
//                        self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
//                        [self.meetingViewController.remoteVideoViewArray addObject:self.meetingViewController.localVideoViewModel];
//                        
//                        NSInteger count = [self.meetingViewController.remoteVideoViewArray count];
//                        NSInteger page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
//                        NSInteger cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
//                        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
//                        [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
//                        
//                        //本地的数据
//                        NSView *videoView = [engine createLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
//                        self.meetingViewController.localVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:videoView];
//                        self.meetingViewController.localVideoViewModel.userID = self.meetingViewController.userID;
//                        self.meetingViewController.localVideoViewModel.userType = RongRTC_User_Normal;
//                        self.meetingViewController.localVideoViewModel.avType = RongRTC_User_Audio_Video;
//                        self.meetingViewController.localVideoViewModel.headerImage = [NSImage imageNamed:@"MeetingMemberNormal"];
//                        
//                        [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
//                        
//                        //当前用户
//                        self.meetingViewController.observerIndex = RongRTC_User_Normal;
//                        NSUserDefaults *settingUserDefaults = [LogonViewController shareSettingUserDefaults];
//                        [settingUserDefaults setObject:@(RongRTC_User_Normal) forKey:Key_Observer];
//                        [settingUserDefaults synchronize];
//                    }
//                }
//            }];
//        }
//            break;
//        case RongRTC_Capacity_Remove:
//        {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.meetingViewController hungupButtonPressed];
//            });
//        }
//            break;
//        default:
//            break;
//    }
//    [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
//}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyUpgradeObserverToNormalUser:(NSString *)userId
{
    if (LarkMeetingManager.observerIndex != RongRTC_User_Observer)
        return;
    
    [self.meetingViewController showMessageAlert:NSLocalizedString(@"meeting_msg_16", nil) title:NSLocalizedString(@"meeting_invite", nil) completedBlock:^(BOOL selected) {
        if (selected)
        {
//            [self.meetingViewController addTipMsg:userId withMessageType:RongRTC_Message_Upgrade];
            [engine answerUpgradeObserverToNormalUser:userId status:selected];
            if ([userId isEqualToString:self.meetingViewController.userID])
            {
                //右侧成员列表
                MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
                NSColor *headColor = [NSColor randomColorForAvatarRBG];
                if (model)
                {
                    model.userType = RongRTC_User_Normal;
                    model.avType = RongRTC_User_Audio_Video;
                    model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
                    model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
                }
                
                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
                
                //视频VideoViewArray中添加
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
                [LarkMeetingManager addRemoteVideoViewArrayObject:self.meetingViewController.localVideoViewModel];
                [LarkMeetingManager addUserIDArrayObject:self.meetingViewController.localVideoViewModel.userID];
                
                NSInteger count = [LarkMeetingManager.remoteVideoViewArray count];
                NSInteger page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
                NSInteger cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
                
                //本地的数据
                NSView *videoView = [engine createLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
                self.meetingViewController.localVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:videoView];
                self.meetingViewController.localVideoViewModel.userID = self.meetingViewController.userID;
                self.meetingViewController.localVideoViewModel.userName = self.meetingViewController.userName;
                self.meetingViewController.localVideoViewModel.userType = RongRTC_User_Normal;
                self.meetingViewController.localVideoViewModel.avType = RongRTC_User_Audio_Video;
                self.meetingViewController.localVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
                self.meetingViewController.localVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
                self.meetingViewController.localVideoViewModel.micImageView = self.meetingViewController.localVideoViewModel.switchHeadIconView;
                
                LarkMeetingManager.observerIndex = RongRTC_User_Normal;
                
                [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl loadCellVideoView:indexPath withUserID:self.meetingViewController.selectedViewModel.userID];
                
//                if (!self.meetingViewController.isSwitchVideoView)
//                    [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
//                else
//                {
//                    [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
//                    AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
//                    [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
//                }
                
                //当前用户
                NSUserDefaults *settingUserDefaults = [LogonViewController shareSettingUserDefaults];
                [settingUserDefaults setObject:@(RongRTC_User_Normal) forKey:Key_Observer];
                [settingUserDefaults synchronize];
                [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToNormal];
                [self.meetingViewController reminderUserDeviceStatus];
                
                if (LarkMeetingManager.is3UseWhiteBoard)
                    [self.meetingViewController.rongRTCEngine requestWhiteBoardURL];
            }
        }
    }];
    
    /*
    [self.meetingViewController startCallingBreath:^(BOOL isAccept) {
        if (isAccept)
        {
//            [engine answerUserRequestResult:userId withAnswerType:RongRTC_Answer_Upgrade withDeviceType:RongRTC_Device_None status:isAccept];
            
            [engine answerUpgradeObserverToNormalUser:userId status:isAccept];
            if ([userId isEqualToString:self.meetingViewController.userID])
            {
                //右侧成员列表
                for (MeetingVideoViewModel *model in self.meetingViewController.remoteAllMemberArray)
                {
                    if ([model.userID isEqualToString:userId])
                    {
                        model.userType = RongRTC_User_Normal;
                        model.avType = RongRTC_User_Audio_Video;
                        model.headerImage = [NSImage imageNamed:@"MeetingMemberNormal"];
                        break;
                    }
                }
                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
                
                //视频VideoViewArray中添加
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
                [self.meetingViewController.remoteVideoViewArray addObject:self.meetingViewController.localVideoViewModel];
                
                NSInteger count = [self.meetingViewController.remoteVideoViewArray count];
                NSInteger page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
                NSInteger cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
                
                //本地的数据
                NSView *videoView = [engine createLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
                self.meetingViewController.localVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:videoView];
                self.meetingViewController.localVideoViewModel.userID = self.meetingViewController.userID;
                self.meetingViewController.localVideoViewModel.userType = RongRTC_User_Normal;
                self.meetingViewController.localVideoViewModel.avType = RongRTC_User_Audio_Video;
                self.meetingViewController.localVideoViewModel.headerImage = [NSImage imageNamed:@"MeetingMemberNormal"];
                
                [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
                
                //当前用户
                self.meetingViewController.observerIndex = RongRTC_User_Normal;
                NSUserDefaults *settingUserDefaults = [LogonViewController shareSettingUserDefaults];
                [settingUserDefaults setObject:@(RongRTC_User_Normal) forKey:Key_Observer];
                [settingUserDefaults synchronize];
            }
        }
    }];
     [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
    */
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyRemoveUser:(NSString *)userId
{
    [self.meetingViewController messageAlertNo];
    [self.meetingViewController messageAlertSingleYes];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.meetingViewController hungupButtonPressed];
    });
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyScreenSharing:(NSString *)userId open:(BOOL)isOpen
{
    MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
    if (model)
    {
        if (isOpen)
            [self.meetingViewController addTipMsg:model.userName withMessageType:RongRTC_Message_StartScreenShare];
        else
            [self.meetingViewController addTipMsg:model.userName withMessageType:RongRTC_Message_StopScreenShare];
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyNormalUserRequestHostAuthority:(NSString *)userId
{
    DLog(@"LLH...... onNotifyNormalUserRequestHostAuthority: %@", userId);
    
    if (![userId isEqualToString:self.meetingViewController.userID] && LarkMeetingManager.observerIndex == RongRTC_User_Host)
        [self.meetingViewController endBreathTimerAction];
    
    if ([userId isEqualToString:self.meetingViewController.userID] && LarkMeetingManager.observerIndex == RongRTC_User_Normal)
    {
        LarkMeetingManager.observerIndex = RongRTC_User_Host;
        [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToHost];
    }
    else if (LarkMeetingManager.observerIndex != RongRTC_User_Observer)
    {
        LarkMeetingManager.observerIndex = RongRTC_User_Normal;
        [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToNormal];
    }
    
    for (MeetingVideoViewModel *model in LarkMeetingManager.remoteAllMemberArray)
    {
        if (model.userType == RongRTC_User_Host)
            model.userType = RongRTC_User_Normal;
        
        if ([model.userID isEqualToString:userId])
            model.userType = RongRTC_User_Host;
        
//        NSColor *headColor = [NSColor randomColorForAvatarRBG];
//        model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
//        model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
        [self.meetingViewController addTipMsg:model.userName withMessageType:RongRTC_Message_Host];
    }
    
    [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyObserverRequestBecomeNormalUser:(NSString *)userId authorityType:(RongRTC_Authority_Type)type
{
    DLog(@"LLH...... onNotifyObserverRequestBecomeNormalUser: %@  authorityType:%zd", userId, type);
    
    if ([self.meetingViewController.userID isEqualToString:userId])
        return;
    
    switch (LarkMeetingManager.observerIndex) //当前用户角色
    {
        case RongRTC_User_Normal: //正常用户
            break;
        case RongRTC_User_Observer: //观察者
            break;
        case RongRTC_User_Host: //主持人
        {
            if (LarkMeetingManager.observerIndex != RongRTC_User_Host)
                return;
            
            if ([LarkMeetingManager.handupRequestArray count] > 0)
            {
                [engine answerObserverRequestBecomeNormalUser:userId status:RongRTC_Accept_Busy];
                return;
            }
            else
                [LarkMeetingManager addHandupRequestArrayObject:userId];
            
            dispatch_async(queue, ^{
                DLog(@"LLH...... 998   userID: %@", userId);
                self.meetingViewController.requestUpgradeUserID = userId;
                [self observerRequestBecomeNormalUser:userId rongRTCEngine:engine];
                dispatch_semaphore_wait(self.meetingViewController.semaphore, DISPATCH_TIME_FOREVER);
            });
        }
            break;
        default:
            break;
    }
    
    return;
}

- (void)observerRequestBecomeNormalUser:(NSString *)userId rongRTCEngine:(RongRTCEngine *)engine
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.meetingViewController startCallingBreath:userId block:^(BOOL isAccept) {
            [engine answerObserverRequestBecomeNormalUser:userId status:isAccept];
            [LarkMeetingManager removeHandupRequestArrayObject:userId];
            dispatch_semaphore_signal(self.meetingViewController.semaphore);
        }];
    });
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyHostControlUserDevice:(NSString *)userId host:(NSString *)hostID deviceType:(RongRTC_Device_Type)dType open:(BOOL)isOpen
{
    if (isOpen) //邀请打开
    {
        if ([userId isEqualToString:self.meetingViewController.userID])
        {
            if ([LarkMeetingManager isPromoteCapacityArrayContainObject:@(dType)])
                return;
            else
                [LarkMeetingManager addPromoteCapacityArrayObject:@(dType)];
            
            dispatch_async(queue, ^{
                DLog(@"LLH...... 998   userID: %@", userId);
                [self promptCapacity:engine withUserID:userId deviceType:dType open:isOpen];
                dispatch_semaphore_wait(self.meetingViewController.semaphore, DISPATCH_TIME_FOREVER);
            });
        }
    }
    else //直接关闭
    {
        [engine answerHostControlUserDevice:userId withDeviceType:dType open:isOpen status:YES];
        [self updateUser:userId deviceType:dType open:isOpen];
    }
}

- (void)promptCapacity:(RongRTCEngine *)engine withUserID:(NSString *)userId deviceType:(RongRTC_Device_Type)dType open:(BOOL)isOpen
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *message, *title;
        switch (dType)
        {
            case RongRTC_Device_Camera:
            {
                title = NSLocalizedString(@"meeting_camear", nil);
                message = NSLocalizedString(@"meeting_msg_17", nil);
            }
                break;
            case RongRTC_Device_Micphone:
            {
                title = NSLocalizedString(@"meeting_micphone", nil);
                message = NSLocalizedString(@"meeting_msg_18", nil);
            }
                break;
            case RongRTC_Device_CameraMicphone:
            {
                title = NSLocalizedString(@"meeting_camera_micphone", nil);
                message = NSLocalizedString(@"meeting_msg_19", nil);
            }
                break;
            default:
            {
                title = @"打开";
                message = NSLocalizedString(@"meeting_msg_20", nil);
            }
                break;
        }
        
        [self.meetingViewController showMessageAlert:message title:title completedBlock:^(BOOL selected)
         {
             [engine answerHostControlUserDevice:userId withDeviceType:dType open:isOpen status:selected];
             [LarkMeetingManager removePromoteCapacityArrayObject:@(dType)];
             dispatch_semaphore_signal(self.meetingViewController.semaphore);
             
             if (selected)
                 [self updateUser:userId deviceType:dType open:isOpen];
         }];
    });
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onHostControlUserDevice:(NSString *)userID withDeviceType:(RongRTC_Device_Type)dType responseCode:(NSInteger)code
{
    /*
    if (code != 0)
        return;
    
    if (self.meetingViewController.observerIndex == RongRTC_User_Host)
    {
        for (MeetingVideoViewModel *model in self.meetingViewController.remoteAllMemberArray)
        {
            if ([userID isEqualToString:model.userID])
            {
                [self adaptUserType:dType withDataModel:model open:NO];
                break;
            }
        }
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
    }
    
    for (MeetingVideoViewModel *tempModel in self.meetingViewController.remoteVideoViewArray)
    {
        if ([tempModel.userID isEqualToString:userID])
        {
            [self adaptUserType:dType withDataModel:tempModel open:NO];
            RongRTCVideoViewDisplayType type;
            if (tempModel.cellVideoView.frame.size.width == kVideoWidth && tempModel.cellVideoView.frame.size.height == kVideoHeight)
                type = RongRTC_VideoViewDisplay_CompleteView;
            else
                type = RongRTC_VideoViewDisplay_FullScreen;
            
            NSView *videoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, tempModel.cellVideoView.frame.size.width, tempModel.cellVideoView.frame.size.height) withUserID:userID withDisplayType:type];
            tempModel.cellVideoView = videoView;
            
            if (tempModel.avType == RongRTC_User_Only_Audio || tempModel.avType == RongRTC_User_Audio_Video_None)
            {
                if (!tempModel.micImageView)
                {
                    if (videoView.frame.size.width == kVideoWidth && videoView.frame.size.height == kVideoHeight)
                        tempModel.micImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight)];
                    else
                        tempModel.micImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, kCellSize, kCellSize)];
                }
                else
                {
                    if (videoView.frame.size.width == kVideoWidth && videoView.frame.size.height == kVideoHeight)
                        tempModel.micImageView.frame = CGRectMake((kVideoWidth-kVideoHeight/2)/2, (kVideoHeight-kVideoHeight/2)/2, kVideoHeight/2, kVideoHeight/2);
                    else
                        tempModel.micImageView.frame = CGRectMake(kCellSize/4, kCellSize/4, kCellSize/2, kCellSize/2);
                }
                
                tempModel.micImageView.image = [NSImage imageNamed:@"MeetingCloseCameraHeaderImage"];
                [videoView addSubview:tempModel.micImageView];
            }
            else
            {
                if (tempModel.micImageView)
                    [tempModel.micImageView removeFromSuperview];
            }
        }
    }
     */
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyAnswerUpgradeObserverToNormalUser:(NSString *)userID status:(BOOL)isAccept
{
    //邀请观察者发言, 升级成正常用户
    [LarkMeetingManager addUpgradeUserIDArrayObject:userID];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyAnswerObserverRequestBecomeNormalUser:(NSString *)userID status:(RongRTC_Accept_Type)acceptType
{
    if ([userID isEqualToString:self.meetingViewController.userID] && acceptType == RongRTC_Accept_YES)
    {
        //右侧成员列表
        MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userID];
        NSColor *headColor = [NSColor randomColorForAvatarRBG];
        if (model)
        {
            model.userType = RongRTC_User_Normal;
            model.avType = RongRTC_User_Audio_Video;
            model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
            model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
        }
        
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        
        //视频VideoViewArray中添加
        self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
        [LarkMeetingManager addRemoteVideoViewArrayObject:self.meetingViewController.localVideoViewModel];
        
        NSInteger count = [LarkMeetingManager.remoteVideoViewArray count];
        NSIndexPath *indexPath;
        if (self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage > 0)
        {
            NSInteger page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
            NSInteger cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
            indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
        }
        else
        {
            indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        }
        
        [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
        
        //本地的数据
        NSView *videoView = [engine createLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
        self.meetingViewController.localVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:videoView];
        self.meetingViewController.localVideoViewModel.userID = self.meetingViewController.userID;
        self.meetingViewController.localVideoViewModel.userName = self.meetingViewController.userName;
        self.meetingViewController.localVideoViewModel.userType = RongRTC_User_Normal;
        self.meetingViewController.localVideoViewModel.avType = RongRTC_User_Audio_Video;
        self.meetingViewController.localVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
        self.meetingViewController.localVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
        self.meetingViewController.localVideoViewModel.micImageView = self.meetingViewController.localVideoViewModel.switchHeadIconView;
        
        [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
        [self.meetingViewController.meetingViewBuilder.switchPageViewControl loadCellVideoView:indexPath withUserID:self.meetingViewController.selectedViewModel.userID];
        
        //当前用户
        LarkMeetingManager.observerIndex = RongRTC_User_Normal;
        NSUserDefaults *settingUserDefaults = [LogonViewController shareSettingUserDefaults];
        [settingUserDefaults setObject:@(RongRTC_User_Normal) forKey:Key_Observer];
        [settingUserDefaults synchronize];
        [self.meetingViewController.leftMVControlCollectionViewDelegateSource turnCollectionViewItemToNormal];
        [self.meetingViewController reminderUserDeviceStatus];
        
        if (LarkMeetingManager.is3UseWhiteBoard)
            [self.meetingViewController.rongRTCEngine requestWhiteBoardURL];
    }
    else if (acceptType == RongRTC_Accept_Busy)
        [self.meetingViewController showMessageAlertSingle:NSLocalizedString(@"meeting_msg_21", nil) title:NSLocalizedString(@"meeting_speach_request", nil)];
    else
        self.meetingViewController.requestUpgradeUserID = userID;
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyAnswerHostControlUserDevice:(NSString *)userID withAnswerType:(RongRTC_Answer_Type)type withDeviceType:(RongRTC_Device_Type)dType status:(BOOL)isAccept
{
    if (isAccept)
    {
        BOOL isOpen = NO;
        if (type == RongRTC_Answer_InviteOpen) {
            isOpen = YES;
        }
        else if (type == RongRTC_Answer_InviteClose)
            isOpen = NO;
            
        [self updateUser:userID deviceType:dType open:isOpen];////////////////////
        
//        if (self.meetingViewController.observerIndex == RongRTC_User_Host)
//        {
//            for (MeetingVideoViewModel *model in self.meetingViewController.remoteAllMemberArray)
//            {
//                if ([userID isEqualToString:model.userID])
//                {
//                    BOOL isOpen = NO;
//                    if (type == RongRTC_Answer_InviteOpen)
//                        isOpen = YES;
//                    else if (type == RongRTC_Answer_InviteClose)
//                        isOpen = NO;
//                    
//                    [self adaptUserType:dType withDataModel:model open:isOpen];
//                    break;
//                }
//            }
//            [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
//        }
//        
//        for (MeetingVideoViewModel *tempModel in self.meetingViewController.remoteVideoViewArray)
//        {
//            if ([tempModel.userID isEqualToString:userID])
//            {
//                [self adaptUserType:dType withDataModel:tempModel open:NO];
//                RongRTCVideoViewDisplayType type;
//                if (tempModel.cellVideoView.frame.size.width == kVideoWidth && tempModel.cellVideoView.frame.size.height == kVideoHeight)
//                    type = RongRTC_VideoViewDisplay_CompleteView;
//                else
//                    type = RongRTC_VideoViewDisplay_FullScreen;
//                
//                NSView *videoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, tempModel.cellVideoView.frame.size.width, tempModel.cellVideoView.frame.size.height) withUserID:userID withDisplayType:type];
//                tempModel.cellVideoView = videoView;
//                
//                if (tempModel.avType == RongRTC_User_Only_Audio || tempModel.avType == RongRTC_User_Audio_Video_None)
//                {
//                    if (!tempModel.micImageView)
//                    {
//                        if (videoView.frame.size.width == kVideoWidth && videoView.frame.size.height == kVideoHeight)
//                            tempModel.micImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight)];
//                        else
//                            tempModel.micImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, kCellSize, kCellSize)];
//                    }
//                    else
//                    {
//                        if (videoView.frame.size.width == kVideoWidth && videoView.frame.size.height == kVideoHeight)
//                            tempModel.micImageView.frame = CGRectMake((kVideoWidth-kVideoHeight/2)/2, (kVideoHeight-kVideoHeight/2)/2, kVideoHeight/2, kVideoHeight/2);
//                        else
//                            tempModel.micImageView.frame = CGRectMake(kCellSize/4, kCellSize/4, kCellSize/2, kCellSize/2);
//                    }
//                    
//                    tempModel.micImageView.image = [NSImage imageNamed:@"MeetingCloseCameraHeaderImage"];
//                    [videoView addSubview:tempModel.micImageView];
//                }
//                else
//                {
//                    if (tempModel.micImageView)
//                        [tempModel.micImageView removeFromSuperview];
//                }
//            }
//        }
    }
    else
    {
        DLog(@"LLH...... 通知主持人, 对方不同意打开");
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyAnswerDegradeNormalUserToObserver:(NSString *)userId status:(BOOL)isAccept
{
    if (isAccept)
    {
        //右侧成员列表
        MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userId];
        if (model)
        {
            model.userType = RongRTC_User_Observer;
            model.avType = RongRTC_User_Audio_Video;
            NSColor *headColor = [NSColor observerColor];
            model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
            model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
        }
        
        [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
        
        //视频需要在此处被移除
        if ([LarkMeetingManager.userIDArray indexOfObject:userId] != NSNotFound)
        {
            MeetingVideoViewModel *model = [LarkMeetingManager getRemoteVideoViewArrayObject:userId];
            if (model)
                [self.meetingViewController addTipMsg:model.userName withMessageType:RongRTC_Message_Leave];
            
            NSInteger index = [LarkMeetingManager indexOfRemoteVideoViewArrayUser:userId];
            NSInteger page = index / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
            NSInteger cell = fmod(index, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
            if (LarkMeetingManager.isSwitchVideoView && LarkMeetingManager.selectedSwitchCellIndex == cell)
            {
                [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
            }
            
            {
                [LarkMeetingManager removeObjectFromRemoteVideoViewArrayAtIndex:index];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:indexPath];
                
//                self.meetingViewController.meetingViewBuilder.localVideoView = [[rongRTCEngine sharedrongRTCEngine] changeLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
//                AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
//                [appDelegate.childWindow.contentView addSubview:self.meetingViewController.meetingViewBuilder.localVideoView];
            }
        }
        
        if (LarkMeetingManager.observerIndex == RongRTC_User_Observer)
        {
            if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
            {
                [self.meetingViewController.localVideoViewModel.cellVideoView removeFromSuperview];
                
                if ([LarkMeetingManager.remoteVideoViewArray count] > 0)
                {
                    self.meetingViewController.localVideoViewModel = LarkMeetingManager.remoteVideoViewArray[0];
                    [LarkMeetingManager removeObjectFromRemoteVideoViewArrayAtIndex:0];
//                    [self.meetingViewController.userIDArray removeObjectAtIndex:0];
                    //                [weakChatVC.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
                    [self.meetingViewController.meetingViewBuilder.switchPageViewControl removeCellView:[NSIndexPath indexPathForItem:0 inSection:0]];
                    
                    self.meetingViewController.localVideoViewModel.cellVideoView = [engine changeRemoteVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
                    self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
                    AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
                    [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
                }
                else if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:userId])
                {
                    self.meetingViewController.localVideoViewModel = nil;
                }
            }
        }
        
        //        [self updateUser:userId deviceType:dType open:isAccept];
    }
    else
    {
        DLog(@"LLH...... 通知主持人, 对方不同意关闭");
    }
}

- (void)rongRTCEngine:(RongRTCEngine *)engine user:(NSString *)userID answerType:(RongRTC_Answer_Type)type deviceType:(RongRTC_Device_Type)dType accept:(BOOL)isAccept
{
    switch (type)
    {
        case RongRTC_Answer_Upgrade: //邀请观察者发言, 升级成正常用户
        {
            if ([userID isEqualToString:self.meetingViewController.userID] && isAccept)
            {
            }
        }
            break;
        case RongRTC_Answer_Normal: //观察者主动要求发言, 升级成正常用户
        {
            if ([userID isEqualToString:self.meetingViewController.userID] && isAccept)
            {
                //右侧成员列表
                MeetingVideoViewModel *model = [LarkMeetingManager getRemoteAllMemberArrayObject:userID];
                NSColor *headColor = [NSColor randomColorForAvatarRBG];
                if (model)
                {
                    model.userType = RongRTC_User_Normal;
                    model.avType = RongRTC_User_Audio_Video;
                    model.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:model.userName withColor:headColor];
                    model.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:model.userName withColor:headColor];
                }
                
                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
                
                //视频VideoViewArray中添加
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
                [LarkMeetingManager addRemoteVideoViewArrayObject:self.meetingViewController.localVideoViewModel];
                
                NSInteger count = [LarkMeetingManager.remoteVideoViewArray count];
                NSInteger page = (count-1) / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
                NSInteger cell = fmod(count-1, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cell inSection:page];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl addNewCellView:indexPath];
                
                //本地的数据
                NSView *videoView = [engine createLocalVideoViewFrame:CGRectMake(0, 0, kVideoWidth, kVideoHeight) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
                self.meetingViewController.localVideoViewModel = [[MeetingVideoViewModel alloc] initWithView:videoView];
                self.meetingViewController.localVideoViewModel.userID = self.meetingViewController.userID;
                self.meetingViewController.localVideoViewModel.userName = self.meetingViewController.userName;
                self.meetingViewController.localVideoViewModel.userType = RongRTC_User_Normal;
                self.meetingViewController.localVideoViewModel.avType = RongRTC_User_Audio_Video;
                self.meetingViewController.localVideoViewModel.headIconView = [[HeadIconView alloc] initWithFrame:RightTableHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
                self.meetingViewController.localVideoViewModel.switchHeadIconView = [[HeadIconView alloc] initWithFrame:SwitchPageHeaderFrame withUserName:self.meetingViewController.localVideoViewModel.userName withColor:headColor];
                self.meetingViewController.localVideoViewModel.micImageView = self.meetingViewController.localVideoViewModel.switchHeadIconView;
                
                [self.meetingViewController.meetingSwitchPageViewDelegateSource switchPageViewControl:self.meetingViewController.meetingViewBuilder.switchPageViewControl didSelectedCellAtIndexPath:indexPath];
                [self.meetingViewController.meetingViewBuilder.switchPageViewControl loadCellVideoView:indexPath withUserID:self.meetingViewController.selectedViewModel.userID];
                
                //当前用户
                LarkMeetingManager.observerIndex = RongRTC_User_Normal;
                NSUserDefaults *settingUserDefaults = [LogonViewController shareSettingUserDefaults];
                [settingUserDefaults setObject:@(RongRTC_User_Normal) forKey:Key_Observer];
                [settingUserDefaults synchronize];
            }
        }
            break;
        case RongRTC_Answer_InviteOpen: //邀请打开
        {
            if (isAccept)
                [self updateUser:userID deviceType:dType open:isAccept];
            else
            {
                DLog(@"LLH...... 通知主持人, 对方不同意打开");
            }
        }
            break;
        default:
            break;
    }
}

- (void)rongRTCEngineOnAudioDeviceReady:(RongRTCEngine *)engine
{
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onRemoteVideoView:(NSView *)videoView vidoeSize:(CGSize)size remoteUserID:(NSString*)userId
{
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNetworkSentLost:(NSInteger)lost
{
    if (0 <= lost && lost <= 5)
        [self.meetingViewController modifySignalLevel:5];
    else if (5 < lost && lost <= 15)
        [self.meetingViewController modifySignalLevel:4];
    else if (15 < lost && lost <= 30)
        [self.meetingViewController modifySignalLevel:3];
    else if (30 < lost && lost <= 45)
        [self.meetingViewController modifySignalLevel:2];
    else if (45 < lost)
        [self.meetingViewController modifySignalLevel:1];
}

- (void)rongRTCEngine:(RongRTCEngine *)engine onNotifyUserAudioLevel:(NSArray *)levelArray
{
//    DLog(@"LLH...... AudioLevel: %@", levelArray);
    NSInteger myAudioLevel = [levelArray[0][@"audioleval"] integerValue];
    if (LarkMeetingManager.isSwitchVideoView)
    {
        if (!self.meetingViewController.localVideoViewModel.audioLevelView)
        {
            self.meetingViewController.localVideoViewModel.audioLevelView = [[NSView alloc] initWithFrame:CGRectMake(0, 0, kCellSize, kCellAudioViewHeight)];
            self.meetingViewController.localVideoViewModel.audioLevelView.wantsLayer = YES;
        }
        
        if (myAudioLevel > 0 && !LarkMeetingManager.is7Mute)
        {
            NSString *alName = [NSString stringWithFormat:@"MeetingAudioLevel%zd", myAudioLevel];
            self.meetingViewController.localVideoViewModel.audioLevelView.layer.contents = [NSImage imageNamed:alName];
            self.meetingViewController.localVideoViewModel.audioLevelView.layer.hidden = NO;
            [self.meetingViewController.localVideoViewModel.cellVideoView addSubview:self.meetingViewController.localVideoViewModel.audioLevelView];
        }
        else
        {
            self.meetingViewController.localVideoViewModel.audioLevelView.layer.contents = nil;
            self.meetingViewController.localVideoViewModel.audioLevelView.layer.hidden = YES;
            [self.meetingViewController.localVideoViewModel.audioLevelView removeFromSuperview];
        }
    }
    else
    {
        if (myAudioLevel > 0 && !LarkMeetingManager.is7Mute)
        {
            NSString *alName = [NSString stringWithFormat:@"MeetingAudioLevel%zd", myAudioLevel];
            self.meetingViewController.meetingViewBuilder.mainAudioLevelImageView.image = [NSImage imageNamed:alName];
            self.meetingViewController.meetingViewBuilder.mainAudioLevelImageView.hidden = NO;
        }
        else
            self.meetingViewController.meetingViewBuilder.mainAudioLevelImageView.hidden = YES;
    }
    
    for (MeetingVideoViewModel *model in LarkMeetingManager.remoteVideoViewArray)
    {
        for (NSInteger i = 1; i < [levelArray count]; i++)
        {
            NSString *tmpUserID = levelArray[i][@"userid"];
            if ([model.userID isEqualToString:tmpUserID])
            {
                NSInteger audioLevel = [levelArray[i][@"audioleval"] integerValue];
                if (model.cellVideoView.frame.size.width == kCellSize)
                {
                    if (!model.audioLevelView)
                    {
                        model.audioLevelView = [[NSView alloc] initWithFrame:CGRectMake(0, 0, kCellSize, kCellAudioViewHeight)];
                        model.audioLevelView.wantsLayer = YES;
                        
                        for (SwitchPageViewCell *cell in self.meetingViewController.meetingViewBuilder.switchPageViewControl.remoteVideoCellArray)
                        {
                            if ([cell.userID isEqualToString:tmpUserID])
                                [cell.videoView addSubview:model.audioLevelView];
                        }
                    }
                    
                    if (audioLevel > 0)
                    {
                        NSString *alName = [NSString stringWithFormat:@"MeetingAudioLevel%zd", audioLevel];
                        model.audioLevelView.layer.contents = [NSImage imageNamed:alName];
                        model.audioLevelView.layer.hidden = NO;
                    }
                    else
                    {
                        model.audioLevelView.layer.contents = nil;
                        model.audioLevelView.layer.hidden = YES;
                    }
                }
                else
                {
                    if (audioLevel > 0)
                    {
                        NSString *alName = [NSString stringWithFormat:@"MeetingAudioLevel%zd", audioLevel];
                        self.meetingViewController.meetingViewBuilder.mainAudioLevelImageView.image = [NSImage imageNamed:alName];
                        self.meetingViewController.meetingViewBuilder.mainAudioLevelImageView.hidden = NO;
                    }
                    else
                        self.meetingViewController.meetingViewBuilder.mainAudioLevelImageView.hidden = YES;
                }
                model.audioLevel = audioLevel;
            }
        }
    }
}

- (void)updateUser:(NSString *)userId deviceType:(RongRTC_Device_Type)dType open:(BOOL)isOpen
{
    if ([userId isEqualToString:self.meetingViewController.userID])
    {
        [self adaptUserType:dType withDataModel:self.meetingViewController.localVideoViewModel open:isOpen];
        //此处刷新自己的视频
        [self.meetingViewController updateAudioVideoType:self.meetingViewController.localVideoViewModel.avType];
    }
    else
    {
        if (LarkMeetingManager.observerIndex == RongRTC_User_Observer)
        {
            [self adaptUserType:dType withDataModel:self.meetingViewController.localVideoViewModel open:isOpen];
            [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
            //此处刷新远端视频
            [self rongRTCEngine:self.meetingViewController.rongRTCEngine onUser:userId audioVideoType:self.meetingViewController.localVideoViewModel.avType];
        }
        else
        {
            MeetingVideoViewModel *model = [LarkMeetingManager getRemoteVideoViewArrayObject:userId];
            if (model)
            {
                [self adaptUserType:dType withDataModel:model open:isOpen];
                [self.meetingViewController.meetingViewBuilder.rightTableView reloadData];
                //此处刷新远端视频
                [self rongRTCEngine:self.meetingViewController.rongRTCEngine onUser:userId audioVideoType:model.avType];
//                    [self.meetingViewController.rongRTCEngine controlAudioVideoDevice:dType open:isOpen];
            }
        }
    }
}

- (void)adaptUserType:(RongRTC_Device_Type)dType withDataModel:(MeetingVideoViewModel *)model open:(BOOL)isOpen
{
    switch (model.avType)
    {
        case RongRTC_User_Only_Audio:
        {
            switch (dType)
            {
                case RongRTC_Device_Camera:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Only_Audio;
                    break;
                case RongRTC_Device_Micphone:
                    model.avType = isOpen ? RongRTC_User_Only_Audio : RongRTC_User_Audio_Video_None;
                    break;
                case RongRTC_Device_CameraMicphone:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Audio_Video_None;
                    break;
                default:
                    break;
            }
        }
            break;
        case RongRTC_User_Audio_Video:
        {
            switch (dType)
            {
                case RongRTC_Device_Camera:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Only_Audio;
                    break;
                case RongRTC_Device_Micphone:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Only_Video;
                    break;
                case RongRTC_Device_CameraMicphone:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Audio_Video_None;
                    break;
                default:
                    break;
            }
        }
            break;
        case RongRTC_User_Only_Video:
        {
            switch (dType)
            {
                case RongRTC_Device_Camera:
                    model.avType = isOpen ? RongRTC_User_Only_Video : RongRTC_User_Audio_Video_None;
                    break;
                case RongRTC_Device_Micphone:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Only_Video;
                    break;
                case RongRTC_Device_CameraMicphone:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Audio_Video_None;
                    break;
                default:
                    break;
            }
        }
            break;
        case RongRTC_User_Audio_Video_None:
        {
            switch (dType)
            {
                case RongRTC_Device_Camera:
                    model.avType = isOpen ? RongRTC_User_Only_Video : RongRTC_User_Audio_Video_None;
                    break;
                case RongRTC_Device_Micphone:
                    model.avType = isOpen ? RongRTC_User_Only_Audio : RongRTC_User_Audio_Video_None;
                    break;
                case RongRTC_Device_CameraMicphone:
                    model.avType = isOpen ? RongRTC_User_Audio_Video : RongRTC_User_Audio_Video_None;
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}


@end
